<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>XGPlayer本地视频播放器</title>
  <style>
    html,
    body {
      width: 100%;
      height: 100%;
      margin: auto;
      overflow: hidden;
    }

    .fancy-btn {
      position: relative;
      padding: 4px 8px;
      border: none;
      background-color: #3f51b5;
      color: #fff;
      font-size: 12px;
      font-weight: 500;
      letter-spacing: 0.5px;
      border-radius: 4px;
      overflow: hidden;
      cursor: pointer;
      transition: background-color 0.3s ease;
    }

    .fancy-btn:hover {
      background-color: #5c6bc0;
    }

    .fancy-btn-trail {
      position: absolute;
      top: 0;
      left: -100%;
      width: 100%;
      height: 100%;
      background-color: rgba(255, 255, 255, 0.2);
      transform: skewX(-25deg);
      transition: left 0.5s ease;
    }

    .fancy-btn:hover .fancy-btn-trail {
      left: 100%;
    }

    .fancy-btn-trail-fill {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(255, 255, 255, 0.1);
      transform: skewX(25deg);
    }

    #fileName {
      display: inline-block;
      max-width: 660px;
      /* 溢出显示点点点 */
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  </style>
  <script src="//unpkg.byted-static.com/xgplayer/2.31.2/browser/index.js" type="text/javascript"></script>
</head>

<body>
  <div id="topArea">
    <a href="https://v2.h5player.bytedance.com/generate/">文档地址</a>
    <button id="playLocalVideo">播放本地视频</button>
    <button id="playFolderVideos" style="display:none;">播放文件夹视频</button>
    <span id="operatarea"></span>
    <span id="fileName"></span><!-- 播放视频的文件名 -->
  </div>

  <div id="xgplayer"></div>
  <input type="file" id="localVideo" style="display:none;">
  <input type="file" id="folderVideos" multiple style="display:none;" accept=".mp4" />
  <script>
    const defaultPlaybackRate = 2;// 初始化播放速度
    var player = new Player({
      id: 'xgplayer',
      lang: 'zh-cn',
      // url: 'http://localhost:3010/xgplayer-demo-360p.mp4',
      url: 'http://',// url地址不能为空
      // playbackRate: [0.5, 0.75, 1, 1.5, 2, 2.5, 3].reverse(),
      playbackRate: [3, 2.5, 2.25, 2, 1.5, 1, 0.75, 0.5],
      autoplay: true,
      volume: 0.5,
      defaultPlaybackRate: defaultPlaybackRate,
      keyShortcut: 'on',
      keyShortcutStep: {
        currentTime: 10,// 快进10秒
      },
      width: '100%',
      // height: '480px'
      height: 'calc(100% - 24.5px)'
    })

    var operatarea = document.getElementById('operatarea');
    var beishu = [4, 3, 2.5, 2.25, 2, 1.75, 1.5, 1.25, 1, 0.75, 0.5];
    // 生成按钮，并且绑定事件
    // beishu.forEach(item => {
    //   var button = document.createElement('button');
    //   button.innerText = item;
    //   button.style.marginRight = '5px';
    //   button.addEventListener('click', () => {
    //     player.playbackRate = item;
    //   });
    //   operatarea.appendChild(button);
    // })

    beishu.forEach(item => {
      var button = document.createElement('button');
      button.className = 'fancy-btn';
      button.innerHTML = `<span>${item}</span>
        <div class="fancy-btn-trail">
          <div class="fancy-btn-trail-fill"></div>
        </div>`;
      button.style.marginRight = '5px';
      button.addEventListener('click', () => {
        player.playbackRate = item;
      });
      operatarea.appendChild(button);
    })


    var huanman = [-0.1, 0.1];
    huanman.forEach(item => {
      var button = document.createElement('button');
      button.innerText = item + "倍";
      button.style.marginRight = '5px';
      button.addEventListener('click', () => {
        player.playbackRate += item;
      });
      operatarea.appendChild(button);
    })


    var kuaijin = [-30, -20, -10, 10, 20, 30];
    kuaijin.forEach(item => {
      var button = document.createElement('button');
      button.innerText = item + '秒';
      button.style.marginRight = '5px';
      button.addEventListener('click', () => {
        player.currentTime = player.currentTime + item;
        // player.play();
      });
      operatarea.appendChild(button);
    })

    document.getElementById('playLocalVideo').addEventListener('click', () => {
      document.getElementById('localVideo').click();
    });

    document.getElementById('localVideo').addEventListener('change', (e) => {
      const file = e.target.files[0];
      if (file) {
        console.log(file);
        document.getElementById('fileName').innerText = file.name;
        const url = URL.createObjectURL(file);
        // const url = 'http://localhost:3010/低代码培训/' + file.name;
        // const url = 'http://localhost:3010/' + file.name;
        // http://localhost:3010/低代码培训/4.表单的功能讲解.mp4
        player.src = url;
        player.playbackRate = defaultPlaybackRate;
        player.play();
      }
    });

    document.getElementById('playFolderVideos').addEventListener('click', () => {
      document.getElementById('folderVideos').click();
    });

    document.getElementById('folderVideos').addEventListener('change', (e) => {
      const files = e.target.files;
      if (files.length > 0) {
        let index = 0;
        const playNextVideo = () => {
          if (index < files.length) {
            const file = files[index];
            const url = URL.createObjectURL(file);
            player.src = url;
            player.play();
            index++;
          }
        };
        player.on('ended', playNextVideo);
        playNextVideo();
      }
    });
  </script>
</body>

</html>